import echarts from 'echarts';

// export default {};
/**
 * bScore 图表

 */
export function getBChartOption(_obj, _type) {
  let chartOption = {};
  let chartTitle = '';
  let timelineData = [];
  let lineTypeOptions = [];
  function getArr(mInd, _index) {
    let _result = [];
    _obj['ranks' + mInd].forEach(function(i, index) {
      if (index != 0) {
        _result.push(i[_index]);
      }
    });
    // console.log(_result);
    return _result;
  }
  function getSingleLineSeries() {
    //获取公共series
    let rArr = [];
    _obj.ranks1[0].forEach(function(i, index) {
      if (index != 0) {
        rArr.push({
          name: i,
          type: 'line',
          smooth: true,
        });
      }
    });
    return rArr;
  }
  if (_type) {
    if (_type == 'overall') {
      chartTitle = '财务异常值综合评分';
      timelineData = ['综合评分', '利润表', '现金流量表', '资产负债表'];
      lineTypeOptions = [
        {
          series: [
            {
              data: getArr('1', 1),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#1890FF',
                  },
                },
              },
            },
            {
              data: getArr('1', 2),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#2FC25B',
                  },
                },
              },
            },
            {
              data: getArr('1', 3),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#EC8B23',
                  },
                },
              },
            },
            {
              data: getArr('1', 4),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#37cccc',
                  },
                },
              },
            },
            {
              data: getArr('1', 5),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#65b4ff',
                  },
                },
              },
            },
          ],
        },
        {
          series: [
            {
              data: getArr('2', 1),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#1890FF',
                  },
                },
              },
            },
            {
              data: getArr('2', 2),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#2FC25B',
                  },
                },
              },
            },
            {
              data: getArr('2', 3),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#EC8B23',
                  },
                },
              },
            },
            {
              data: getArr('2', 4),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#37cccc',
                  },
                },
              },
            },
            {
              data: getArr('2', 5),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#65b4ff',
                  },
                },
              },
            },
          ],
        },
        {
          series: [
            {
              data: getArr('3', 1),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#1890FF',
                  },
                },
              },
            },
            {
              data: getArr('3', 2),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#2FC25B',
                  },
                },
              },
            },
            {
              data: getArr('3', 3),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#EC8B23',
                  },
                },
              },
            },
            {
              data: getArr('3', 4),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#37cccc',
                  },
                },
              },
            },
            {
              data: getArr('3', 5),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#65b4ff',
                  },
                },
              },
            },
          ],
        },
        {
          series: [
            {
              data: getArr('4', 1),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#1890FF',
                  },
                },
              },
            },
            {
              data: getArr('4', 2),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#2FC25B',
                  },
                },
              },
            },
            {
              data: getArr('4', 3),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#EC8B23',
                  },
                },
              },
            },
            {
              data: getArr('4', 4),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#37cccc',
                  },
                },
              },
            },
            {
              data: getArr('4', 5),
              type: 'line',
              itemStyle: {
                normal: {
                  lineStyle: {
                    color: '#65b4ff',
                  },
                },
              },
            },
          ],
        },
      ];
    }
  }

  let sLineSeries;
  sLineSeries = getSingleLineSeries();

  function getSingleLineLegend() {
    //获取公共legend名称
    let rArr = [];
    // console.log(rArr);
    _obj.ranks1[0].forEach(function(i, index) {
      if (index != 0) {
        rArr.push(i);
      }
    });
    // console.log(rArr);
    return rArr;
  }

  chartOption = {
    baseOption: {
      title: {
        text: chartTitle,
        left: 'center',
      },
      dataZoom: [
        {
          type: 'inside',
          start: 50,
          end: 100,
        },
      ],
      grid: {
        left: '0%',
        right: '0%',
        bottom: '20%',
        containLabel: true,
      },
      timeline: {
        axisType: 'category',
        autoPlay: false,
        data: timelineData,
      },
      tooltip: { trigger: 'axis' },
      legend: {
        x: 'center',
        width: '100%',
        top: '30px',
        data: getSingleLineLegend(),
      },
      xAxis: [
        {
          type: 'category',
          boundaryGap: false,
          data: getArr('1', 0),
          axisLabel: {
            interval: 3,
            rotate: 0,
          },
        },
      ],
      yAxis: {
        type: 'value',
      },
      series: sLineSeries,
    },
    color: ['#1890FF', '#2FC25B', '#D8A874', '#37cccc', '#65b4ff'],
    options: lineTypeOptions,
  };
  return chartOption;
}

export function getMscoreOption(_objArr, _title, _ifArea, _textBox) {
  let chart_option = {};
  let chart_title = _title;
  let xAxisData = [];
  function getArrSeries(_arr) {
    let seriesResult = [];
    _arr.forEach((cellObj, index) => {
      let _data = [];
      for (let key in cellObj.value) {
        if (index == 0) {
          xAxisData.push(key);
        }
        _data.push(cellObj.value[key]);
        // console.log(key,cellObj.value[key]);
      }
      if (_ifArea) {
        seriesResult.push({
          name: cellObj.name,
          data: _data,
          type: 'line',
          markArea: {
            silent: true,
            data: [
              [
                {
                  name: _textBox[1],
                  yAxis: 0,
                },
                {
                  yAxis: 1,
                  label: {
                    position: ['47%', '50%'],
                    color: '#ffffff',
                    textBorderColor: 'rgb(192,53,90)',
                    textBorderWidth: 2,
                    fontSize: 14,
                  },
                  itemStyle: {
                    color: 'rgba(247,191,132,0.57)',
                  },
                },
              ],
              [
                {
                  name: _textBox[0],
                  yAxis: 1,
                },
                {
                  y: 'max',
                  label: {
                    position: ['47%', '50%'],
                    color: '#ffffff',
                    textBorderColor: 'rgb(192,53,90)',
                    textBorderWidth: 2,
                    fontSize: 14,
                  },
                  itemStyle: {
                    color: 'rgba(142,234,169,0.57)',
                  },
                },
              ],
            ],
            label: {
              show: true,
              fontSize: 18,
            },
          },
        });
      } else {
        seriesResult.push({
          name: cellObj.name,
          type: 'line',
          data: _data,
        });
      }
    });
    // console.log(seriesResult);
    return seriesResult;
  }

  chart_option = {
    title: {
      text: chart_title,
      left: 'center',
    },
    color: ['#2FC25B', '#1890FF', '#49efda', '#e8a13c', '#fb7396'],
    tooltip: {
      trigger: 'axis',
    },
    grid: {
      left: '0',
      right: '0',
      bottom: '0',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: xAxisData,
      axisLabel: {
        interval: 0,
        rotate: 30,
      },
    },
    yAxis: {
      type: 'value',
    },
    series: getArrSeries(_objArr),
  };
  // console.log(chart_option);
  return chart_option;
}
export function getAScoreOption(_objArr, _title, _ifShowArea) {
  let chart_option = {};
  let chart_title = _title;
  function getArrSeries(_arr) {
    let _result = [];
    _arr.forEach(function(i, index) {
      let helperObj;
      if (_ifShowArea && index == 0) {
        helperObj = {
          name: '',
          data: [],
          type: 'line',
          markArea: {
            silent: true,
            data: [
              [
                {
                  name: '破产区',
                  y: 'min',
                },
                {
                  yAxis: 1.81,
                  label: {
                    position: ['47%', '50%'],
                    color: '#ffffff',
                    textBorderColor: 'rgb(192,53,90)',
                    textBorderWidth: 2,
                    fontSize: 14,
                  },
                  itemStyle: {
                    color: 'rgba(247,191,132,0.57)',
                  },
                },
              ],
              [
                {
                  name: '灰色区',
                  yAxis: 1.81,
                },
                {
                  yAxis: 2.675,
                  label: {
                    position: ['47%', '50%'],
                    color: '#ffffff',
                    textBorderColor: 'rgb(241,209,90)',
                    textBorderWidth: 2,
                    fontSize: 14,
                  },
                  itemStyle: {
                    color: 'rgba(241,209,199,0.5)',
                  },
                },
              ],
              [
                {
                  name: '安全区',
                  yAxis: 2.675,
                },
                {
                  y: 'max',
                  label: {
                    position: ['47%', '50%'],
                    color: '#ffffff',
                    textBorderColor: 'rgb(192,53,90)',
                    textBorderWidth: 2,
                    fontSize: 14,
                  },
                  itemStyle: {
                    color: 'rgba(142,234,169,0.57)',
                  },
                },
              ],
            ],
          },
        };
      } else {
        helperObj = {
          name: '',
          data: [],
          type: 'line',
        };
      }

      helperObj.name = i.name;
      helperObj.data = i.value;
      _result.push(helperObj);
    });
    return _result;
  }

  chart_option = {
    title: {
      text: chart_title,
      left: 'center',
    },
    color: ['#2FC25B', '#1890FF', '#49efda', '#e8a13c', '#fb7396'],
    tooltip: {
      trigger: 'axis',
    },
    grid: {
      left: '0',
      right: '0',
      bottom: '0',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: _objArr.x,
      axisLabel: {
        interval: 1,
        rotate: 30,
      },
    },
    yAxis: {
      type: 'value',
    },
    series: getArrSeries(_objArr.data),
  };
  return chart_option;
}
// 数组格式数据转折线图
export function getLineOption(_title, _obj) {
  let _series = [];
  let _legends = [];
  let _xAixsData = _obj.dates;
  for (let i = 0; i < _obj.data.length; i++) {
    let thisSer = {
      name: _obj.data[i].name,
      type: 'line',
      symbol: 'none',
      data: _obj.data[i].data,
    };
    _legends.push(_obj.data[i].name);
    _series.push(thisSer);
  }

  let _option = {
    title: {
      text: _title,
    },
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      icon: 'circle',
      itemWidth: 4,
      itemHeight: 4,
      data: _legends,
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: _xAixsData,
    },
    yAxis: {
      type: 'value',
    },
    series: _series,
  };

  return _option;
}

export function getBarSimple(_title, _obj) {
  let xAxisData = [];
  let seriesData = [];
  for (let i = 0; i < _obj.length; i++) {
    xAxisData.push(_obj[i].sources);
    seriesData.push(_obj[i].value);
  }
  let optionResult = {
    xAxis: {
      type: 'category',
      data: xAxisData,
      boundaryGap: true,
      axisTick: {
        alignWithLabel: true,
      },
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        barWidth: 35,
        data: seriesData,
        type: 'bar',
        itemStyle: {
          normal: {
            color: '#d99f62',
          },
        },
      },
    ],
  };

  return optionResult;
}

export function getSimpleBar(_title, _xAxisData, _data) {
  let optionResult = {
    xAxis: {
      type: 'category',
      data: _xAxisData,
    },
    yAxis: {
      type: 'value',
    },
    series: [
      {
        color: '#D99F62',
        barWidth: 35,
        data: _data,
        type: 'bar',
      },
    ],
  };

  return optionResult;
}

export function getStackBar(_title, _obj) {
  let _series = [];
  _obj.y.forEach((_cell, _index) => {
    _series.push({
      name: _obj.legends[_index],
      barWidth: 35,
      data: _cell,
      type: 'bar',
      stack: 'common',
      markLine: {
        lineStyle: {
          normal: {
            type: 'dashed',
          },
        },
        data: [[{ type: 'min' }, { type: 'max' }]],
      },
    });
  });
  let optionResult = {
    xAxis: {
      type: 'category',
      data: _obj.x,
    },
    legend: {
      data: _obj.legends,
      icon: 'circle',
      itemWidth: 4,
      itemHeight: 4,
    },
    yAxis: {
      type: 'value',
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        // 坐标轴指示器，坐标轴触发有效
        type: 'shadow', // 默认为直线，可选为：'line' | 'shadow'
      },
    },
    color: ['#79604B', '#D28249', '#998246', '#E8B256', '#B06049', '#D89F61'],
    series: _series,
  };
  console.log(optionResult);
  return optionResult;
}

export function getMapOption(_title, _obj) {
  let optionResult = {
    backgroundColor: '#FFFFFF',
    title: {
      text: _title,
      subtext: '',
      x: 'center',
    },
    tooltip: {
      trigger: 'item',
    },
    visualMap: {
      show: true,
      x: 'left',
      y: 'center',
      color: ['#eaf2fa', '#6fa4dc'],
    },
    series: [
      {
        name: '数据',
        type: 'map',
        mapType: 'china',
        roam: false,
        label: {
          normal: {
            show: true,
          },
          emphasis: {
            show: false,
          },
        },
        data: _obj, //数据
      },
    ],
  };

  return optionResult;
}

export function getPieOption(_title, _obj) {
  let _option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)',
    },
    series: [
      {
        color: ['#d4755a', '#5a98d4'],
        name: '情绪敏感度',
        type: 'pie',
        radius: ['50%', '70%'],
        avoidLabelOverlap: false,
        labelLine: {
          normal: {
            show: true,
          },
        },
        data: _obj,
      },
    ],
  };
  return _option;
}

export function getMarginFinanceOption(_title, _objArr) {
  let _xAixsData = [];
  let _seriesData = [];
  for (let i = 0; i < _objArr.length; i++) {
    _xAixsData.push(_objArr[i].trading_day);
    _seriesData.push(_objArr[i].trading_value);
  }
  let _option = {
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: _xAixsData,
    },
    legend: {
      icon: 'circle',
      itemWidth: 4,
      itemHeight: 4,
      data: _xAixsData,
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        formatter: value => {
          var result = value / 100000000 + '亿';
          return result;
        },
      },
    },
    series: [
      {
        data: _seriesData,
        type: 'line',
        itemStyle: {
          normal: {
            color: '#D99F62', //圈圈的颜色
            lineStyle: {
              color: '#D99F62', //线的颜色
            },
          },
        },
        areaStyle: {
          normal: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              { offset: 0, color: '#D99F62' },
              { offset: 1, color: '#ffffff' },
            ]),
          },
        },
      },
    ],
  };

  return _option;
}

export function getEquityOption(_title, _obj) {
  let _option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)',
    },
    legend: {
      orient: 'vertical',
      x: 'left',
      data: ['质押股权', '质押股权2'],
    },
    series: [
      {
        name: '占总股本',
        type: 'pie',
        radius: ['50%', '70%'],
        avoidLabelOverlap: false,
        label: {
          normal: {
            show: false,
            position: 'center',
          },
          emphasis: {
            show: true,
            textStyle: {
              fontSize: '30',
              fontWeight: 'bold',
            },
          },
        },
        labelLine: {
          normal: {
            show: false,
          },
        },
        data: [{ value: 32, name: '质押股权' }, { value: 68, name: '质押股权2' }],
      },
    ],
    color: ['#CC5C3D', '#E1E6EC'],
  };

  return _option;
}
